package com.dragee.modules.work.controller;

import cn.hutool.core.util.StrUtil;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.entity.UserEntity;
import com.dragee.modules.sys.service.UserService;
import com.dragee.modules.work.entity.JoinEntity;
import com.dragee.modules.work.service.JoinService;
import com.dragee.modules.work.service.TrainService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * 报名
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-24 11:32:01
 */
@RestController
@RequestMapping("work/approveJoin")
@Api(tags = "work - 审批培训报名")
public class ApproveJoinController {
    @Autowired
    private JoinService joinService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("审批列表")
    public R list(@RequestParam Map<String, Object> params){
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        UserEntity user = userService.getById(loginUser.getUserId());
        String province = user.getProvince();
        if (StrUtil.isNotBlank(province)) {
            params.put("province", province);
        }
        params.put("status", "待审核");
        PageUtils page = joinService.queryPage(params);
        return R.ok().put("data", page);
    }

    @GetMapping("submitApprove")
    @ApiOperation("提交审核")
    public R submitApprove (@RequestParam("id") String id,
                  @RequestParam("result") String result,
                  String rejectCause){
        JoinEntity join = joinService.getById(id);
        if (result.equals("yes")) {
            join.setStatus("已通过");
        } else if (result.equals("no")) {
            join.setStatus("已退回");
            join.setRejectCause(rejectCause);
        }
        joinService.saveOrUpdate(join);
        return R.ok();
    }








}
